package com.imedway.ihe.dtz.xds.registry.camel;

import java.util.ArrayList;
import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryError30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
//import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Folder;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.SubmissionSet;
import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;
import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.imedway.ihe.dtz.xds.cons.SysConsts;
import com.imedway.ihe.dtz.xds.registry.sub.camel.Iti42SubRegistry_CAMEL;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class Iti42Registry_CAMEL {
	
	@Autowired
	private Iti42SubRegistry_CAMEL iti42SubRegistry_CAMEL;

	public RegistryResponseType run(SubmitObjectsRequest body) {
		
		RegistryResponseType registryResponseType = new RegistryResponseType();
//		EbXMLSubmitObjectsRequest30 ebXMLbody= new EbXMLSubmitObjectsRequest30(body);
//		registryResponseType.setRegistryErrorList(new RegistryErrorList());
		List<EbXMLRegistryError> registryError = new ArrayList<EbXMLRegistryError>();
		EbXMLRegistryResponse30 ebXMLRegistryResponse30 = new EbXMLRegistryResponse30(registryResponseType);
		try {
			
			RegisterDocumentSet registerDocumentSet = EbXML30Converters.convert(body);
			List<DocumentEntry> documentEntryList= registerDocumentSet.getDocumentEntries();
			boolean flag=false;
			for(DocumentEntry documentEntry:documentEntryList) {
				log.info(documentEntry.getType().getUuid());
				if(documentEntry.getType()!=null&&SysConsts.DocumentEntryObjectTypeForOnDemandDocumentEntries.equals(documentEntry.getType().getUuid())) {
					flag=true;
					break;
				}
			}
			log.info(String.valueOf(flag));
			if(flag) {
				genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
						"hava no Stable Document Entries");	
				ebXMLRegistryResponse30.setErrors(registryError);
			}else {
				//校验id
		        if(checkPatientID(registerDocumentSet)) {
		    		genError(registryError, Severity.ERROR, ErrorCode.PATIENT_ID_DOES_NOT_MATCH.getOpcode(),
							"PATIENT_ID_DOES_NOT_MATCH");	
		        }
		        
				iti42SubRegistry_CAMEL.run(ebXMLRegistryResponse30,registerDocumentSet);
			}
			
										
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
					e.getMessage());	
			ebXMLRegistryResponse30.setErrors(registryError);
		}
		if(registryError!=null&&registryError.size()>0) {
			ebXMLRegistryResponse30.setErrors(registryError);
		}
		ebXMLRegistryResponse30.setStatus(registryError!=null&&registryError.isEmpty() ? Status.SUCCESS : Status.FAILURE);
		
		return ebXMLRegistryResponse30.getInternal();
	}
	public boolean checkPatientID(RegisterDocumentSet registerDocumentSet) {
		SubmissionSet submissionSet = registerDocumentSet.getSubmissionSet();
		String patientId = "";
		boolean flag = false;
		if(submissionSet!=null) {
			Identifiable submissionSetPatientId = submissionSet.getPatientId();
			if(submissionSetPatientId!=null) {
				log.info(patientId);
				patientId=submissionSetPatientId.getId();
			}
		}	
		List<DocumentEntry> documentEntryList= registerDocumentSet.getDocumentEntries();
		if(documentEntryList!=null) {
			for(DocumentEntry documentEntry:documentEntryList) {
				Identifiable documentEntryPatientId = documentEntry.getPatientId();
				if(documentEntryPatientId!=null&&!patientId.equals(documentEntryPatientId.getId())) {
					flag = true;
					return flag;
				}
			}
		}
		
		List<Folder> folderList = registerDocumentSet.getFolders();
		if(folderList!=null) {
			for(Folder folder:folderList) {
				Identifiable folderPatientId = folder.getPatientId();
				if(folderPatientId!=null&&!patientId.equals(folderPatientId.getId())) {
					flag = true;
					return flag;
				}
			}
		}
		return flag;
		
	}
	private void genError(List<EbXMLRegistryError> registryError,Severity severity,String errorCode,String codeContext) {
		EbXMLRegistryError30 re = new EbXMLRegistryError30(new RegistryError());
		re.setSeverity(severity);
		re.setErrorCode(errorCode);
		re.setCodeContext(codeContext);
		registryError.add(re);
	}
}
